package com.base_data_handler.ak_xnradar;

import java.io.File;
import java.util.logging.Logger;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base_data_handler.function.weather.RealForcastLifeWarnFromChinaWeather;
import com.base_data_handler.utils.HttpRequest;
import com.base_data_handler.utils.ImgScal;
import com.base_data_handler.utils.ftp.FtpConfig;
import com.base_data_handler.utils.ftp.FtpUtil;
/**
 * 安康西南雷达爬取
 * @param urlStr
 * @return
 */
public class China_Radar implements Job {

	private static final Logger log = Logger.getLogger(China_Radar.class.getName());
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		// 调度任务参数
//		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
//		String xxx = (String) dataMap.get("xxx");
			
		try {
			log.info("安康西南雷达图获取radarsigle");
			getImg();
		} catch (Exception e) {
		   log.info("安康西南雷达图获取radarsigle" + e.getMessage());
			e.printStackTrace();
		}
		
	}
	
	@SuppressWarnings("static-access")
	static boolean getImg(){
		//获取并解码中国天气网雷达图片
		 String str = HttpRequest.gizpContent("http://d1.weather.com.cn/radar/JC_RADAR_CHN_JB.html?jsoncallback=readerinfo&dataType=jsonp&cache=!1",
				 "jsoncallback=readerinfo&dataType=jsonp&cache=!1",
				 1, null, null);
//		  爬取解码后的中国天气网雷达数据
//		 readerinfo({"cn":"中国","ys":"雷达基本反射率","radars":[{'fn':'ebref_achn','ft':'20170622133600001','dt':'2017-06-22 21:36'},{'fn':'ebref_achn','ft':'20170622134200001','dt':'2017-06-22 21:42'},{'fn':'ebref_achn','ft':'20170622134800001','dt':'2017-06-22 21:48'},{'fn':'ebref_achn','ft':'20170622135400001','dt':'2017-06-22 21:54'},{'fn':'ebref_achn','ft':'20170622140000001','dt':'2017-06-22 22:00'},{'fn':'ebref_achn','ft':'20170622140600001','dt':'2017-06-22 22:06'},{'fn':'ebref_achn','ft':'20170622141200001','dt':'2017-06-22 22:12'},{'fn':'ebref_achn','ft':'20170622141800001','dt':'2017-06-22 22:18'},{'fn':'ebref_achn','ft':'20170622142400001','dt':'2017-06-22 22:24'},{'fn':'ebref_achn','ft':'20170622143000001','dt':'2017-06-22 22:30'},{'fn':'ebref_achn','ft':'20170622143600001','dt':'2017-06-22 22:36'},{'fn':'ebref_achn','ft':'20170622144200001','dt':'2017-06-22 22:42'},{'fn':'ebref_achn','ft':'20170622144800001','dt':'2017-06-22 22:48'},{'fn':'ebref_achn','ft':'20170622145400001','dt':'2017-06-22 22:54'},{'fn':'ebref_achn','ft':'20170622150000001','dt':'2017-06-22 23:00'},{'fn':'ebref_achn','ft':'20170622150600001','dt':'2017-06-22 23:06'},{'fn':'ebref_achn','ft':'20170622151200001','dt':'2017-06-22 23:12'},{'fn':'ebref_achn','ft':'20170622151800001','dt':'2017-06-22 23:18'},{'fn':'ebref_achn','ft':'20170622152400001','dt':'2017-06-22 23:24'},{'fn':'ebref_achn','ft':'20170622153000001','dt':'2017-06-22 23:30'},{'fn':'ebref_achn','ft':'20170622153600001','dt':'2017-06-22 23:36'},{'fn':'ebref_achn','ft':'20170622154200001','dt':'2017-06-22 23:42'},{'fn':'ebref_achn','ft':'20170622154800001','dt':'2017-06-22 23:48'},{'fn':'ebref_achn','ft':'20170622155400001','dt':'2017-06-22 23:54'},{'fn':'ebref_achn','ft':'20170622160000001','dt':'2017-06-23 00:00'},{'fn':'ebref_achn','ft':'20170622160600001','dt':'2017-06-23 00:06'},{'fn':'ebref_achn','ft':'20170622161200001','dt':'2017-06-23 00:12'},{'fn':'ebref_achn','ft':'20170622161800001','dt':'2017-06-23 00:18'},{'fn':'ebref_achn','ft':'20170622162400001','dt':'2017-06-23 00:24'},{'fn':'ebref_achn','ft':'20170622163000001','dt':'2017-06-23 00:30'},{'fn':'ebref_achn','ft':'20170622163600001','dt':'2017-06-23 00:36'},{'fn':'ebref_achn','ft':'20170622164200001','dt':'2017-06-23 00:42'},{'fn':'ebref_achn','ft':'20170622164800001','dt':'2017-06-23 00:48'},{'fn':'ebref_achn','ft':'20170622165400001','dt':'2017-06-23 00:54'},{'fn':'ebref_achn','ft':'20170622170000001','dt':'2017-06-23 01:00'},{'fn':'ebref_achn','ft':'20170622170600001','dt':'2017-06-23 01:06'},{'fn':'ebref_achn','ft':'20170622171200001','dt':'2017-06-23 01:12'},{'fn':'ebref_achn','ft':'20170622171800001','dt':'2017-06-23 01:18'},{'fn':'ebref_achn','ft':'20170622172400001','dt':'2017-06-23 01:24'},{'fn':'ebref_achn','ft':'20170622173000001','dt':'2017-06-23 01:30'},{'fn':'ebref_achn','ft':'20170622173600001','dt':'2017-06-23 01:36'},{'fn':'ebref_achn','ft':'20170622174200001','dt':'2017-06-23 01:42'},{'fn':'ebref_achn','ft':'20170622174800001','dt':'2017-06-23 01:48'},{'fn':'ebref_achn','ft':'20170622175400001','dt':'2017-06-23 01:54'},{'fn':'ebref_achn','ft':'20170622180000001','dt':'2017-06-23 02:00'},{'fn':'ebref_achn','ft':'20170622180600001','dt':'2017-06-23 02:06'},{'fn':'ebref_achn','ft':'20170622181200001','dt':'2017-06-23 02:12'},{'fn':'ebref_achn','ft':'20170622181800001','dt':'2017-06-23 02:18'},{'fn':'ebref_achn','ft':'20170622182400001','dt':'2017-06-23 02:24'},{'fn':'ebref_achn','ft':'20170622183000001','dt':'2017-06-23 02:30'},{'fn':'ebref_achn','ft':'20170622183600001','dt':'2017-06-23 02:36'},{'fn':'ebref_achn','ft':'20170622184200001','dt':'2017-06-23 02:42'},{'fn':'ebref_achn','ft':'20170622184800001','dt':'2017-06-23 02:48'},{'fn':'ebref_achn','ft':'20170622185400001','dt':'2017-06-23 02:54'},{'fn':'ebref_achn','ft':'20170622190000001','dt':'2017-06-23 03:00'},{'fn':'ebref_achn','ft':'20170622190600001','dt':'2017-06-23 03:06'},{'fn':'ebref_achn','ft':'20170622191200001','dt':'2017-06-23 03:12'},{'fn':'ebref_achn','ft':'20170622191800001','dt':'2017-06-23 03:18'},{'fn':'ebref_achn','ft':'20170622192400001','dt':'2017-06-23 03:24'},{'fn':'ebref_achn','ft':'20170622193000001','dt':'2017-06-23 03:30'},{'fn':'ebref_achn','ft':'20170622193600001','dt':'2017-06-23 03:36'},{'fn':'ebref_achn','ft':'20170622194200001','dt':'2017-06-23 03:42'},{'fn':'ebref_achn','ft':'20170622194800001','dt':'2017-06-23 03:48'},{'fn':'ebref_achn','ft':'20170622195400001','dt':'2017-06-23 03:54'},{'fn':'ebref_achn','ft':'20170622200000001','dt':'2017-06-23 04:00'},{'fn':'ebref_achn','ft':'20170622200600001','dt':'2017-06-23 04:06'},{'fn':'ebref_achn','ft':'20170622201200001','dt':'2017-06-23 04:12'},{'fn':'ebref_achn','ft':'20170622201800001','dt':'2017-06-23 04:18'},{'fn':'ebref_achn','ft':'20170622202400001','dt':'2017-06-23 04:24'},{'fn':'ebref_achn','ft':'20170622203000001','dt':'2017-06-23 04:30'},{'fn':'ebref_achn','ft':'20170622203600001','dt':'2017-06-23 04:36'},{'fn':'ebref_achn','ft':'20170622204200001','dt':'2017-06-23 04:42'},{'fn':'ebref_achn','ft':'20170622204800001','dt':'2017-06-23 04:48'},{'fn':'ebref_achn','ft':'20170622205400001','dt':'2017-06-23 04:54'},{'fn':'ebref_achn','ft':'20170622210000001','dt':'2017-06-23 05:00'},{'fn':'ebref_achn','ft':'20170622210600001','dt':'2017-06-23 05:06'},{'fn':'ebref_achn','ft':'20170622211200001','dt':'2017-06-23 05:12'},{'fn':'ebref_achn','ft':'20170622211800001','dt':'2017-06-23 05:18'},{'fn':'ebref_achn','ft':'20170622212400001','dt':'2017-06-23 05:24'},{'fn':'ebref_achn','ft':'20170622213000001','dt':'2017-06-23 05:30'},{'fn':'ebref_achn','ft':'20170622213600001','dt':'2017-06-23 05:36'},{'fn':'ebref_achn','ft':'20170622214200001','dt':'2017-06-23 05:42'},{'fn':'ebref_achn','ft':'20170622214800001','dt':'2017-06-23 05:48'},{'fn':'ebref_achn','ft':'20170622215400001','dt':'2017-06-23 05:54'},{'fn':'ebref_achn','ft':'20170622220000001','dt':'2017-06-23 06:00'},{'fn':'ebref_achn','ft':'20170622220600001','dt':'2017-06-23 06:06'},{'fn':'ebref_achn','ft':'20170622221200001','dt':'2017-06-23 06:12'},{'fn':'ebref_achn','ft':'20170622221800001','dt':'2017-06-23 06:18'},{'fn':'ebref_achn','ft':'20170622222400001','dt':'2017-06-23 06:24'},{'fn':'ebref_achn','ft':'20170622223000001','dt':'2017-06-23 06:30'},{'fn':'ebref_achn','ft':'20170622223600001','dt':'2017-06-23 06:36'},{'fn':'ebref_achn','ft':'20170622224200001','dt':'2017-06-23 06:42'},{'fn':'ebref_achn','ft':'20170622224800001','dt':'2017-06-23 06:48'},{'fn':'ebref_achn','ft':'20170622225400001','dt':'2017-06-23 06:54'},{'fn':'ebref_achn','ft':'20170622230000001','dt':'2017-06-23 07:00'},{'fn':'ebref_achn','ft':'20170622230600001','dt':'2017-06-23 07:06'},{'fn':'ebref_achn','ft':'20170622231200001','dt':'2017-06-23 07:12'},{'fn':'ebref_achn','ft':'20170622231800001','dt':'2017-06-23 07:18'},{'fn':'ebref_achn','ft':'20170622232400001','dt':'2017-06-23 07:24'},{'fn':'ebref_achn','ft':'20170622233000001','dt':'2017-06-23 07:30'},{'fn':'ebref_achn','ft':'20170622233600001','dt':'2017-06-23 07:36'},{'fn':'ebref_achn','ft':'20170622234200001','dt':'2017-06-23 07:42'},{'fn':'ebref_achn','ft':'20170622234800001','dt':'2017-06-23 07:48'},{'fn':'ebref_achn','ft':'20170622235400001','dt':'2017-06-23 07:54'},{'fn':'ebref_achn','ft':'20170623000000001','dt':'2017-06-23 08:00'},{'fn':'ebref_achn','ft':'20170623000600001','dt':'2017-06-23 08:06'},{'fn':'ebref_achn','ft':'20170623001200001','dt':'2017-06-23 08:12'},{'fn':'ebref_achn','ft':'20170623001800001','dt':'2017-06-23 08:18'},{'fn':'ebref_achn','ft':'20170623002400001','dt':'2017-06-23 08:24'},{'fn':'ebref_achn','ft':'20170623003000001','dt':'2017-06-23 08:30'},{'fn':'ebref_achn','ft':'20170623003600001','dt':'2017-06-23 08:36'},{'fn':'ebref_achn','ft':'20170623004200001','dt':'2017-06-23 08:42'},{'fn':'ebref_achn','ft':'20170623004800001','dt':'2017-06-23 08:48'},{'fn':'ebref_achn','ft':'20170623005400001','dt':'2017-06-23 08:54'},{'fn':'ebref_achn','ft':'20170623010000001','dt':'2017-06-23 09:00'},{'fn':'ebref_achn','ft':'20170623010600001','dt':'2017-06-23 09:06'},{'fn':'ebref_achn','ft':'20170623011200001','dt':'2017-06-23 09:12'},{'fn':'ebref_achn','ft':'20170623011800001','dt':'2017-06-23 09:18'},{'fn':'ebref_achn','ft':'20170623012400001','dt':'2017-06-23 09:24'}]})
		
		 //转换成可解析的json字符串数据
		 String json=str.replace("readerinfo(", "").replace(")", "");
		 //json解析
		 JSONObject obj = JSON.parseObject(json);
		 JSONArray jarr = JSON.parseArray(obj.getString("radars"));
		 JSONObject imgjson = JSON.parseObject(jarr.get(jarr.size()-1).toString());
		 
		 String fileTime=imgjson.getString("ft");
		 String ftpPath="radar";
		 String localPath=ftpPath;
		 //获取最新时次时间
//		 System.out.println(imgjson.getString("ft"));
		 //拼接最新雷达图片地址http://pi.weather.com.cn/i/product/pic/l/sevp_aoc_rdcp_sldas_ebref_achn_l88_pi_
		 String url="http://pi.weather.com.cn/i/product/pic/l/sevp_aoc_rdcp_sldas_ebref_aswc_l88_pi_"+fileTime+".png";
		 
		 //下载图片
		 String result=HttpRequest.sendGet(url, null, 2, fileTime+".png", localPath);
		 
		 String relativePath=localPath+"/"+fileTime+".png";
		 File f=new File(relativePath);
		 File tempF=new File(localPath+"/"+"scale");
		 if(!tempF.exists()) tempF.mkdirs();
			 //下载到本地成功后上传到后台
		if(result.equals("ok")) {
			boolean scale = ImgScal.imageHelper.scaleImageWithParams(f.getAbsolutePath(),
					tempF.getPath()+"/"+fileTime+".png", 1200, 1200, false, "png");
			if(scale) {
				
				return	FtpUtil.uploadLocalFile(ftpPath,FtpConfig.FtpUser,FtpConfig.FtpPassword, 
						FtpConfig.FtpHost, FtpConfig.FtpPort,localPath+"/scale",fileTime+".png");	
			 }
			
			}
		
		 return false;
	}
	
	
	public static void main(String[] args) {
		getImg();
	}
	
}
